#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      Student
#
# Created:     16/07/2012
# Copyright:   (c) Student 2012
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import random
strikes = 0
cont = True
rwords = ("hello", "prime", "kitten", "program",
 "apple", "skyscraper", "tower", "zebra", "amazing", "starcraft")#different words to be spelled

def selection():#choosing which word to "solve"
    rit = random.choice(rwords)
    return rit

def guess(inp, list1, list2, strike, check):
    lastfound = 0
    check = 1
    for x in list1:
        if (x==inp):#if letter exists
            check = 0
            list2[list1.index(inp, lastfound)] = inp
            lastfound= list1.index(inp, lastfound)+1
    return check

def endgamewin(booll):
    print("You won!")
    booll = False

def main():#definition of function, parameters go in parenthesis
    print("Welcome to Hangman!")
    starting = True
    while starting:
        strikes = 0
        strlist1 = list(selection())
        displayar =[]
        for x in strlist1:#replacing all the letters with underscores
            displayar.append("_ ")
        count = displayar.count("_ ")
        print(displayar)
        while strikes<5:
            count = displayar.count("_ ")
            checkk = 0
            inputt = input("What's your guess?:")
            checkk = guess(inputt, strlist1, displayar, strikes, checkk)
            if checkk == 0:
                print("\nCorrect.")
            elif  checkk == 1:
                strikes += 1
                print("\nYou got it wrong.", 5-strikes, " strike(s) left.")
            if strikes == 5:
                print("\nYou lost the game")
                print("The word was ",strlist1)
            if (displayar == strlist1):#if the game is finished
                endgamewin(cont)
                print(displayar)
                strikes = 5
            else:
                if strikes != 5:
                    print(displayar)
        yn = 0
        said = input("Would you like to play again?")
        while yn == 0:
            said = input("Would you like to play again?")
            print("\n Would you like to play again?")
            if (said == "yes"):
                print("\nOk, restarting game.")
                yn = 1
            elif (said == "no"):
                print("\nThank you for playing.")
                starting = False
                yn = 1
            else:
                print("\nSorry, please say yes or no.")





if __name__ == '__main__':
    main()

